package Utils;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 数据库连接工具类
 */
public class JDBCUtils {
    /**
     * 获取数据库连接
     * @return connection连接对象
     */
    public static Connection getConnection(){
        Connection connection = null;
        try {
            //1.加载配置文件
            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
            Properties properties = new Properties();
            properties.load(is);
            //2.读取配置文件信息
            String url = properties.getProperty("url");
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            //3.获取数据源
            DataSource dataSource = new MysqlDataSource();
            MysqlDataSource mysqlDataSource = (MysqlDataSource) dataSource;
            mysqlDataSource.setUrl(url);
            mysqlDataSource.setUser(user);
            mysqlDataSource.setPassword(password);
            //4.获取连接
            connection = mysqlDataSource.getConnection();
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 关闭数据库连接资源
     * @param connection connection连接对象
     * @param preparedStatement preparedStatement对象
     */
    public static void closeResource(Connection connection, PreparedStatement preparedStatement){
        try {
            if(connection != null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            if (preparedStatement != null){
                preparedStatement.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 关闭数据库连接资源
     * @param connection connection连接对象
     * @param ps preparedStatement对象集合
     */
    public static void closeResource(Connection connection, PreparedStatement ... ps){
        try {
            if(connection != null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        for (PreparedStatement p : ps) {
            try {
                if (p != null) {
                    p.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 关闭数据库连接资源
     * @param connection connection连接对象
     * @param preparedStatement preparedStatement对象
     * @param resultSet resultSet对象
     */
    public static void closeResource(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
        closeResource(connection,preparedStatement);
        try {
            if(resultSet != null){
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();

        }
    }

    //单元测试
    public static void main(String[] args) {
        Connection connection = getConnection();
        System.out.println(connection);
    }
}
